#ifndef COMMON_H
#define COMMON_H

#include <map>
#include "semaphores.h"

typedef int GrassAmount;

class Meadow
{
	GrassAmount **fields;
	int rows, cols;
	Semaphore sem;
	
	public:
	
	Meadow(int rows, int cols) : sem(1)
	{
		this->rows = rows;
		this->cols = cols;
		this->fields = new GrassAmount*[rows];
		for (int i = 0; i < rows; ++i)
		{
			this->fields[i] = new GrassAmount[cols];
			for (int j = 0; j < cols; ++j)
				this->fields[i][j] = 0;
		}
	}
	
	~Meadow()
	{
		this->sem.P();
		for (int i = 0; i < this->rows; ++i)
			delete [] this->fields[i];
		delete [] this->fields;
		this->sem.V();
	}
	
	GrassAmount getField(int row, int col)
	{
		GrassAmount val;
		this->sem.P();
		val = this->fields[row][col];
		this->sem.V();
		return val;
	}
	
	void setField(int row, int col, GrassAmount val)
	{
		this->sem.P();
		this->fields[row][col] = val;
		this->sem.V();
	}
};

struct Common
{
	Meadow *meadow;
};

#endif
